import Koa from "koa"
import Router from "@koa/router"
// import chalk from "chalk"
import cors from "koa2-cors"

// import { getMasterIp } from "@/utils"

const app = new Koa()

const router = new Router()

// const PORT = 7008

app
  .use(cors())

  .use(router.allowedMethods())

app.on("error", err => {
  console.error("server error", err)
})

const createRouter = () => new Router()

type IRouters = Router.Middleware<Koa.DefaultState, Koa.DefaultContext, unknown>

const mountRouter = <T extends IRouters>(r: T | T[]) => {
  const ruoters = Array.isArray(r) ? r : [r]

  ruoters.forEach(i => app.use(i))
}

export default () => ({
  app,
  router,
  createRouter,
  mountRouter,
})
